from typing import Optional, List

from fastapi import Depends, Query, Body
from fastapi_utils.cbv import cbv
from fastapi_utils.inferring_router import InferringRouter
from sqlalchemy.orm import Session

from server.app.cloud_ide.schema.ide_registry.request.ide_registry_create_request import IdeRegistryCreateRequest
from server.app.cloud_ide.schema.ide_registry.request.ide_registry_update_request import IdeRegistryUpdateRequest
from server.app.cloud_ide.service.ide_registry_service import IdeRegistryService
from server.framework.core.http_schemas import CommonResponseSchema, TableResponseSchema
from server.framework.core.injects import get_db
from server.framework.core.schemas import OptionsSchema

ide_registry_api = InferringRouter()


@cbv(ide_registry_api)
class IdeRegistryApi:
    ide_registry_service = IdeRegistryService()
    db: Session = Depends(get_db)

    @ide_registry_api.get("/get_registry_options", name='获取镜像源下拉选项', response_model=List[OptionsSchema])
    async def get_registry_options(self) -> List[OptionsSchema]:
        results = self.ide_registry_service.get_all_ide_registry(self.db)

        option_results = []
        for row in results:
            option_results.append(OptionsSchema(value=str(row.id), label=row.name))
        return option_results

    @ide_registry_api.get("/get_entity", name='获取单个镜像源')
    async def get_entity(self, id: str = Query(None, description='镜像源id')):
        entity = self.ide_registry_service.get_ide_registry_by_id(self.db, id)
        return entity

    @ide_registry_api.get("/list", response_model=TableResponseSchema, name='镜像源列表')
    async def list(
            self,
            name: str = Query(None, description='过滤条件:镜像源名称'),
            enable: int = Query(None, description='过滤条件:镜像源是否启用(0:禁用,1:启用)'),
            current: Optional[int] = Query(1, description='当前页数'),
            page_size: Optional[int] = Query(10, description='每页记录的数量', alias='pageSize')) -> TableResponseSchema:
        """
        获取镜像源列表
        :return:
        """

        items, total = self.ide_registry_service.get_ide_registry_list(self.db, name=name,
                                                                       enable=enable, page_size=page_size,
                                                                       current_page=current)
        return TableResponseSchema(data=items, total=total, page=page_size, success=True)

    @ide_registry_api.post("/create", response_model=CommonResponseSchema, name='创建镜像源')
    async def create(self, data: IdeRegistryCreateRequest = Body(None, description='镜像源创建请求')) -> CommonResponseSchema:
        """
        创建镜像源
        :return:
        """
        rs = self.ide_registry_service.create_ide_registry(self.db, data)
        if rs:
            return CommonResponseSchema(data='', message=f'创建成功', success=True)
        else:
            return CommonResponseSchema(data='', message=f'创建失败', success=False)

    @ide_registry_api.get("/delete", response_model=CommonResponseSchema, name='删除镜像源')
    async def delete(self,
                     ids: str = Query(None, description='镜像源id，多条记录以逗号分割')) -> CommonResponseSchema:
        delete_ids = ids.split(',')
        rs = self.ide_registry_service.delete_ide_registry(self.db, delete_ids)
        if rs:
            return CommonResponseSchema(data={}, message=f'删除成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'删除失败', success=False)

    @ide_registry_api.post("/edit", response_model=CommonResponseSchema, name='编辑镜像源')
    async def edit(self, data: IdeRegistryUpdateRequest = Body(None, description='镜像源编辑实体')) -> CommonResponseSchema:
        rs = self.ide_registry_service.update_ide_registry(self.db, data)
        if rs:
            return CommonResponseSchema(data={}, message=f'编辑成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'编辑失败', success=False)
